<html>
<head><meta charset="utf-8"><title>comparing Some(T) with T · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html">comparing Some(T) with T</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186198107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186198107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186198107">(Jan 21 2020 at 16:12)</a>:</h4>
<p>Hi,<br>
How do people feel about adding something like <code>impl&lt;T: PartialEq&gt; PartialEq&lt;Option&lt;T&gt;&gt; for T</code> such that <code>Some(5) == 5</code>?</p>
<p>I can PR this but wanted to know if people even want that.<br>
I think it helps ergonomics, especially in tests.</p>



<a name="186198887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186198887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186198887">(Jan 21 2020 at 16:19)</a>:</h4>
<p>I'd say it's worth discussing, but my gut is that people will be naturally wary of it. is there any sort of precedent for this behavior anywhere else in the stdlib?</p>



<a name="186202473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186202473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186202473">(Jan 21 2020 at 16:52)</a>:</h4>
<blockquote>
<p>I'd say it's worth discussing, but my gut is that people will be naturally wary of it. is there any sort of precedent for this behavior anywhere else in the stdlib?</p>
</blockquote>
<p>Yes. <code>IpAddr(Ipv4Addr) == Ipv4Addr</code> it's not exactly the same because there are no user defined generics here, but close enough IMHO</p>



<a name="186213555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186213555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186213555">(Jan 21 2020 at 18:44)</a>:</h4>
<p>How would this feel when comparing an <code>Option&lt;Option&lt;T&gt;&gt;</code> with <code>None</code>?</p>



<a name="186215724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186215724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186215724">(Jan 21 2020 at 19:06)</a>:</h4>
<p>Arguably an IP address that happens to be of v4 variant <em>is</em> an IPv4 address</p>



<a name="186215785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186215785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186215785">(Jan 21 2020 at 19:07)</a>:</h4>
<p>But <code>Option</code> is often used where optionalness is significant</p>



<a name="186376394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186376394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186376394">(Jan 23 2020 at 10:40)</a>:</h4>
<blockquote>
<p>How would this feel when comparing an <code>Option&lt;Option&lt;T&gt;&gt;</code> with <code>None</code>?</p>
</blockquote>
<p>Well that's a great point. sadly I think that point is more than enough to scrap the idea.</p>



<a name="186377516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186377516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186377516">(Jan 23 2020 at 10:58)</a>:</h4>
<p>I still think there's some merit, not as syntax with <code>==</code> but with a method like <code>option.deep_eq(foo)</code> or something, as in my experience <code>option.map_or(false, |x| x == y)</code> is a pretty common pattern when dealing with options.</p>



<a name="186384030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186384030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186384030">(Jan 23 2020 at 12:38)</a>:</h4>
<p>The <code>Option&lt;Option&lt;T&gt;&gt;</code> case also means you might hit trait coherence errors when implementing something like this</p>



<a name="186385193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186385193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186385193">(Jan 23 2020 at 12:53)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> Even in this case?</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">deep_eq</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">rhs</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">lhs</span><span class="o">|</span><span class="w"> </span><span class="n">lhs</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">rhs</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="186385505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186385505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186385505">(Jan 23 2020 at 12:57)</a>:</h4>
<p>That one looks probably fine because it only accepts <code>T</code> as a parameter. I meant for something that wants to accept either <code>T</code> or <code>Option&lt;T&gt;</code>, but even then it depends on the exact formulation</p>



<a name="186399733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186399733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186399733">(Jan 23 2020 at 15:31)</a>:</h4>
<blockquote>
<p>How would this feel when comparing an <code>Option&lt;Option&lt;T&gt;&gt;</code> with <code>None</code>?</p>
</blockquote>
<p>Wouldn't this just be a type inference issue? Once you clarify <code>None::&lt;T&gt;</code> or <code>None::&lt;Option&lt;T&gt;&gt;</code>, it should be fine.</p>



<a name="186400013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/comparing%20Some%28T%29%20with%20T/near/186400013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/comparing.20Some(T).20with.20T.html#186400013">(Jan 23 2020 at 15:33)</a>:</h4>
<p>But the new blanket impl does seem like a breaking change anyway</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>